package com.playball.lottery.frm.score;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.playball.lottery.R;

/**
 * 比分点击筛选按钮后用到的数据适配器
 * 
 * @author Administrator
 * 
 */
public class ScoreFiltergridviewAdapter extends BaseAdapter {
	Context cxt;
	private LayoutInflater inflater;
	private List<Picture> pictures;
	/** 存放选中项赛事简称,默认全部赛事 */
	private List<String> setSelectList = new ArrayList<String>();
	/** 存放按钮选中状态 */
	private List<Map<String, Object>> filter_mData = new ArrayList<Map<String, Object>>();

	public ScoreFiltergridviewAdapter(String[] titles, int[] nums, Context context, List<Map<String, Object>> appData) {
		super();
		cxt = context;
		pictures = new ArrayList<Picture>();
		inflater = LayoutInflater.from(context);
		if (nums != null) {
			for (int i = 0; i < nums.length; i++) {
				Picture picture = new Picture(titles[i], nums[i]);
				pictures.add(picture);
			}
		}
		filter_mData = deepCopy(appData);
		// 存放选中项赛事简称,默认全部选中
		if (setSelectList.size() == 0) {
			for (int i = 0; i < pictures.size(); i++) {
				setSelectList.add(pictures.get(i).getTitle());
			}
		}
	}

	public static <T> List<T> deepCopy(List<T> src) {
		try {
			ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
			ObjectOutputStream out = new ObjectOutputStream(byteOut);
			out.writeObject(src);
			ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
			ObjectInputStream in = new ObjectInputStream(byteIn);
			@SuppressWarnings("unchecked")
			List<T> dest = (List<T>) in.readObject();
			return dest;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获取选择赛事
	 * 
	 * @return
	 */
	public List<String> getseltitleslist() {
		setSelectList.clear();
		for (int i = 0; i < this.filter_mData.size(); i++) {
			if (Integer.parseInt(this.filter_mData.get(i).get("isCheck").toString()) == 1) {
				this.filter_mData.get(i).put("isCheck", 1);
				setSelectList.add(pictures.get(i).getTitle());
			}
		}
		return setSelectList;
	}

	/**
	 * 清除列表中所有赛事
	 */
	public void clearseltitleslist() {
		setSelectList.clear();
	}

	/**
	 * 全选
	 * 
	 * @param data
	 */
	public void setSelectAllData() {
		// TODO Auto-generated method stub
		setSelectList.clear();
		for (int i = 0; i < this.filter_mData.size(); i++) {
			this.filter_mData.get(i).put("isCheck", 1);
		}
		// seltitleslist获取全部
		for (int i = 0; i < pictures.size(); i++) {
			setSelectList.add(pictures.get(i).getTitle());
		}
	}

	/**
	 * 反选
	 * 
	 * @param data
	 */
	public void setSelectOtherData() {
		// TODO Auto-generated method stub
		setSelectList.clear();
		for (int i = 0; i < this.filter_mData.size(); i++) {
			if (Integer.parseInt(this.filter_mData.get(i).get("isCheck").toString()) == 1) {
				this.filter_mData.get(i).put("isCheck", 0);
			} else {
				this.filter_mData.get(i).put("isCheck", 1);
				setSelectList.add(pictures.get(i).getTitle());
			}
		}
		notifyDataSetChanged();
	}

	/**
	 * 全不选
	 * 
	 * @param data
	 */
	public void setSelectNoData() {
		// TODO Auto-generated method stub
		for (int i = 0; i < this.filter_mData.size(); i++) {
			this.filter_mData.get(i).put("isCheck", 0);
		}
		if (setSelectList != null) {
			setSelectList.clear();
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (null != pictures) {
			return pictures.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		return pictures.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public List<Map<String, Object>> getSelectData() {
		return filter_mData;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.scorefilterdialog_grid_item, null);
			viewHolder = new ViewHolder();
			// 组件ID
			viewHolder.RelativeLayout1 = (RelativeLayout) convertView.findViewById(R.id.RelativeLayout1);
			viewHolder.title = (TextView) convertView.findViewById(R.id.text_item);
			viewHolder.num = (TextView) convertView.findViewById(R.id.text_num);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		// 赛事按钮点击与否
		if ((Integer) filter_mData.get(position).get("isCheck") == 0) {
			viewHolder.RelativeLayout1.setBackgroundResource(R.drawable.input_select);// score_filter_btn2
			viewHolder.title.setTextColor(cxt.getResources().getColor(R.color.buy_jxxh_fontcolor_qianhui));
			viewHolder.num.setTextColor(cxt.getResources().getColor(R.color.buy_jxxh_fontcolor_qianhui));
		} else {
			viewHolder.RelativeLayout1.setBackgroundResource(R.drawable.feed_pwd_input);// score_filter_btn
			viewHolder.title.setTextColor(cxt.getResources().getColor(R.color.red_ssqfont));
			viewHolder.num.setTextColor(cxt.getResources().getColor(R.color.red_ssqfont));
		}
		final String title = pictures.get(position).getTitle();
		// 如果赛事简称名字过长，截取前三位
		String tmptitle;
		if (title.length() <= 3) {
			tmptitle = title;
		} else {
			tmptitle = title.substring(0, 3);
		}
		viewHolder.title.setText(tmptitle);
		viewHolder.num.setText("(" + pictures.get(position).getNum() + ")");
		// 按钮点击事件
		viewHolder.RelativeLayout1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if ((Integer) filter_mData.get(position).get("isCheck") == 0) {
					filter_mData.get(position).put("isCheck", 1);
				} else if ((Integer) filter_mData.get(position).get("isCheck") == 1) {
					filter_mData.get(position).put("isCheck", 0);
				}
				// 重新调用getview，不要在此事件直接设置组件的显示
				notifyDataSetChanged();
			}
		});
		return convertView;
	}
}

class ViewHolder {
	public TextView title;
	public TextView num;
	public RelativeLayout RelativeLayout1;
}

class Picture {
	private String title;
	private int num;

	public Picture() {
		super();
	}

	public Picture(String title, int num) {
		super();
		this.title = title;
		this.num = num;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

}
